package com.slkj.shilixiaoyuanapp.ui.parent.tool.exercise;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.ActivityInfo;
import com.slkj.shilixiaoyuanapp.base.BaseActivity;
import com.slkj.shilixiaoyuanapp.data.UserRequest;
import com.slkj.shilixiaoyuanapp.entity.parent.ExerciseHistoryEntity;
import com.slkj.shilixiaoyuanapp.net.HttpHeper;
import com.slkj.shilixiaoyuanapp.net.callback.CommonCallBack;
import com.slkj.shilixiaoyuanapp.view.DividerItemDecoration;
import com.slkj.shilixiaoyuanapp.view.MySwipeRefreshLayout;
import com.slkj.shilixiaoyuanapp.view.StateLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static com.slkj.shilixiaoyuanapp.entity.code.UiCode.PAGE_COUNT;

@ActivityInfo(layout = R.layout.activity_exercise_history,title = "历史记录")
public class ExerciseHistoryActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.recycer)
    RecyclerView recycerView;
    @BindView(R.id.layout_root)
    StateLayout stateLayout;
    BaseQuickAdapter adapter;
    @BindView(R.id.swipeLayout)
    MySwipeRefreshLayout refreshLayout;
    List<ExerciseHistoryEntity> datas=new ArrayList<>();
    int page=1;
    private int count = PAGE_COUNT;
    int stuId=122;
    int classId=21;
    @Override
    protected void init() {
        for (int i=0;i<UserRequest.getInstance().getUser().getStudents().size();i++){
            if (UserRequest.getInstance().getUser().getStudents().get(i).getIsSelect()==1){
                stuId= UserRequest.getInstance().getUser().getStudents().get(i).getId();
                classId= UserRequest.getInstance().getUser().getStudents().get(i).getClassId();
            }
        }
        loadFirstData();
        recycerView.setLayoutManager(new LinearLayoutManager(this));
        recycerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        adapter = new BaseQuickAdapter<ExerciseHistoryEntity, BaseViewHolder>(R.layout.item_exercise_history, datas) {
            @Override
            protected void convert(BaseViewHolder helper, final ExerciseHistoryEntity item) {
                helper.setText(R.id.tv_exercise_type,item.getProject())
                        .setText(R.id.tv_exercise_time,item.getDate());
            }

        };
        adapter.setOnItemClickListener((adapter, view, position) -> {
            //startActivity(HistoryDetailActivity.class);
            HistoryDetailActivity__JumpCenter.builder(this).setId(Integer.parseInt(datas.get(position).getId())).create().go();
        });
        adapter.setOnLoadMoreListener(this::loadMore, recycerView);
        refreshLayout.setOnRefreshListener(this);
        stateLayout.setOnReLoadListener(this::loadFirstData);
        recycerView.setAdapter(adapter);
    }

    private void loadFirstData() {
        refreshLayout.setEnabled(false);
        page = 1;
        HttpHeper.get().parentService()
                .getExerciseHistory(stuId,classId,page, count)
                .compose(bindToLifecycle())
                .compose(getThread())
                .subscribe(new CommonCallBack<List<ExerciseHistoryEntity>>(ExerciseHistoryActivity.this) {
                    @Override
                    public void onCallBackSuccess(List<ExerciseHistoryEntity> data) {
                        datas.addAll(data);
                        refreshLayout.setEnabled(true);
                        stateLayout.showEmptyView();
                        setData(true, data);
                        if (data.size() == 0) {
                            stateLayout.showEmptyView();
                        } else {
                            stateLayout.showContentView();
                        }
                    }
                    @Override
                    public void onFail(String mes) {
                        refreshLayout.setRefreshing(false);
                        stateLayout.showErrorView();
                    }
                });
    }

    private void setData(boolean isRefresh, List<ExerciseHistoryEntity> data) {
        page++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            adapter.setNewData(data);
        } else {
            if (size > 0) {
                adapter.addData(data);
            }
        }
        if (size < count) {
            //第一页如果不够一页就不显示没有更多数据布局
            adapter.loadMoreEnd(isRefresh);
        } else {
            adapter.loadMoreComplete();
        }
    }

    /**
     * 下拉刷新数据刷新数据
     */
    @Override
    public void onRefresh() {
        page = 1;
        //这里的作用是防止下拉刷新的时候还可以上拉加载
        adapter.setEnableLoadMore(false);
        HttpHeper.get().parentService()
                .getExerciseHistory(stuId,classId,page, count)
                .compose(bindToLifecycle())
                .compose(getThread())
                .subscribe(new CommonCallBack<List<ExerciseHistoryEntity>>(ExerciseHistoryActivity.this) {
                    @Override
                    public void onCallBackSuccess(List<ExerciseHistoryEntity> data) {
                        if (data.isEmpty()) {
                            stateLayout.showEmptyView();
                        }
                        setData(true, data);

                        refreshLayout.setRefreshing(false);
                        adapter.setEnableLoadMore(true);
                        stateLayout.showContentView();

                    }

                    @Override
                    public void onFail(String mes) {
                        super.onFail(mes);
                        refreshLayout.setRefreshing(false);
                        adapter.setEnableLoadMore(true);
                        stateLayout.showErrorView();

                    }
                });
    }

    /**
     * 加载更多
     */
    private void loadMore() {
        HttpHeper.get().parentService()
                .getExerciseHistory(stuId,classId,page, count)
                .compose(bindToLifecycle())
                .compose(getThread())
                .subscribe(new CommonCallBack<List<ExerciseHistoryEntity>>() {
                    @Override
                    public void onCallBackSuccess(List<ExerciseHistoryEntity> data) {
                        setData(false, data);
                    }

                    @Override
                    public void onFail(String mes) {
                        adapter.loadMoreFail();
                    }
                });
    }
}
